package com.edu.test;
/*
 * @Author:princess
 * @Date 2023/10/25 10 25 20:52
 *
 */

import com.edu.bean.User;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//通过反射机制来获取class文件中包含哪些方法
public class MyTest2 {
    @Test
    public void test1(){
        Class clazz = User.class;
        Method[] methods = clazz.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());//获取方法名字
            System.out.println(method.getReturnType());//获取方法返回值类型
            System.out.println();
        }
    }

    @Test
    public void test2() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        User user = new User();
        Class clazz = user.getClass();
        Method method = clazz.getDeclaredMethod("sayHello");
        method.setAccessible(true);
        method.invoke(user);

        System.out.println();

        Method method2 = clazz.getDeclaredMethod("sayHi", String.class);
        Object o = method2.invoke(user, "你好");
        System.out.println(o);
    }
}
